import java.util.Scanner;

public class Code {

    //用递归写出斐波那契数列,求第n个
    public static int Fibonacci(int n){
        if(n==1 || n==2){
           return 1;
        }
        return Fibonacci(n-1)+Fibonacci(n-2);
    }

    //斐波那契适合用循环
    public static int fib(int n){
        if(n==1 || n==2){
            return 1;
        }
        int f1 = 1;
        int f2 = 1;
        int f3 = 0;
        for (int i = 3; i <= n ; i++) {
            f3 = f1+f2;
            f1 = f2;
            f2 = f3;
        }
        return f3;
    }

    public static void main(String[] args) {
        System.out.println(Fibonacci(5));
        System.out.println(fib(5));
    }



    //写一个递归方法，输入一个非负整数，返回组成它的数字之和. 例如，输入 1729, 则应该返回
    //1+7+2+9，它的和是19
    public static int s_sum(int i){
        if(i<10){
            return i;
        }
        return (i%10)+s_sum(i/10);
    }

    public static void main8(String[] args) {
        System.out.println(s_sum(1729));
    }




    //用递归求1+2+3+4+……+10
    public static int sum(int j){
        if(j==1){
           return 1;
        }
         return j + sum(j-1);
    }

    public static void main7(String[] args) {
        System.out.println(sum(10));
    }



    //按顺序打印一个数字的每一位，例如1234打印出1 2 3 4
    public static void shun(int n){
         if(n<10){
             System.out.println(n);
         }
         else{
             shun(n/10);
             System.out.println(n%10);
         }
    }

    public static void main6(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int i = scanner.nextInt();
        shun(i);
    }




    //用递归求5的阶乘
    public static int fac2(int n){
        if(n==1){
            return 1;
        }
            return n*fac2(n-1);
    }

    public static void main5(String[] args) {
        System.out.println(fac2(5));
    }




public static int add(int a,int b){
    return a+b;
}
public static double add(double a,double b){
    return a+b;
}

    public static void main4(String[] args) {
        System.out.println(add(4.0, 6.0));
        System.out.println(add(4, 6));
    }




    //用方法调用的方式求5阶乘的累积和
    public static int fac(int n){
        int ret = 1;
        for(int i=1;i<=n;i++){
            ret*=i;
        }
        return ret;
    }
    public static int facSum(int k){
        int sum = 0;
        for(int j = 1;j<=k;j++){
            sum+=fac(j);
        }
        return sum;
    }

    public static void main3(String[] args) {
        System.out.println(facSum(5));
    }

        public static int add1(int x,int y){
            return x+y;
        }

        public static boolean isLeapYear(int year){
            if(((year % 4 == 0) && (year % 100 != 0)) || year % 400 == 0){
                return true;
            }
            return false;
        }

    public static void main2(String[] args) {
        int ret = add1(2,7);
        System.out.println(ret);
        boolean ret1 = isLeapYear(2024);
        System.out.println(ret1);
    }

    public static void main1(String[] args) {
        for (int i = 0; i < args.length; i++) {
            System.out.println(args[i]);

        }
    }
}
